package git.soulbgm.controller;

import cn.hutool.core.date.DateTime;
import git.soulbgm.common.Constants;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 发送数据控制器
 *
 * @author SoulBGM
 * @date 2024-11-11
 */
@RestController
public class SendDataController {

    private static final Random RANDOM = new Random();
    private final KafkaTemplate<String, String> kafka;
    private static final AtomicLong SEQ = new AtomicLong();

    public SendDataController(KafkaTemplate<String, String> kafka) {
        this.kafka = kafka;
    }

    @GetMapping("send")
    public String send(int count) {
        for (int i = 0; i < count; i++) {
            int priority = RANDOM.nextInt(9) + 1;
            String time = new DateTime().toMsStr();
            // kafka.send(Constants.TOPIC_SEND_DATA_PREFIX + priority, SEQ.getAndIncrement() + " : " + time);
            kafka.send(Constants.TOPIC_SEND_DATA, priority + " : " + SEQ.getAndIncrement() + " : " + time);
        }
        return "SUCCESS";
    }

}
